package org.example.genweb.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.catalina.valves.rewrite.ResolverImpl;
import org.example.genweb.entity.User;
import org.example.genweb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
@Tag(name = "用户controller")
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(summary = "添加用户信息")
    @PostMapping
    public User createUser(@RequestBody User user) {
        userService.save(user);
        return user;
    }

    @Operation(summary = "获取用户信息列表")
    @GetMapping
    public List<User> getAllUsers() {
        return userService.list();
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getById(id);
    }

    @PostMapping("update")
    public User updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        userService.updateById(user);
        return user;
    }

    @PostMapping("del/{id}")
    public void deleteUser(@PathVariable Long id) {
        userService.removeById(id);
    }
}